package com.fbli.test.service.impl;

import com.fbli.test.common.entity.projectIter.ProjectIterAddMsg;
import com.fbli.test.common.entity.projectIter.ProjectIterMsg;
import com.fbli.test.entity.Project;
import com.fbli.test.entity.ProjectIter;
import com.fbli.test.mapper.ProjectIterMapper;
import com.fbli.test.service.ProjectIterService;
import com.fbli.test.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectIterServiceImpl implements ProjectIterService {
    @Autowired
    ProjectIterMapper projectIterMapper;
    @Autowired
    ProjectService projectService;
    @Override
    public boolean add(ProjectIterAddMsg addMsg) {
        ProjectIter projectIter=new ProjectIter();
        List<Project> byId = projectService.getById(-1, addMsg.getProjectId());
        if (byId==null){
            return false;
        }
        projectIter.setName(addMsg.getIterName());
        projectIter.setStartTime(addMsg.getStartTime());
        projectIter.setEndTime(addMsg.getEndTime());
        projectIter.setProjectId(addMsg.getProjectId());
        projectIter.setState(1);//未开始
        try {
            projectIterMapper.insert(projectIter);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean update(ProjectIterMsg projectIterMsg) {
        ProjectIter projectIter=new ProjectIter();
        ProjectIter projectIter1 = projectIterMapper.selectByPrimaryKey(projectIterMsg.getIterId());
        if (projectIter1==null) return false;
        projectIter.setId(projectIterMsg.getIterId());
        projectIter.setState(projectIterMsg.getState());
        projectIter.setStartTime(projectIterMsg.getStartTime());
        projectIter.setEndTime(projectIterMsg.getEndTime());
        projectIter.setName(projectIterMsg.getIterName());
        try {
            projectIterMapper.updateByPrimaryKeySelective(projectIter);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public ProjectIter selectById(long iterId) {
       return projectIterMapper.selectByPrimaryKey(iterId);
    }
}
